#ifndef UTIL_H
#define UTIL_H


#include "qlabel.h"
#include <QPaintEvent>
#include "qtimer.h"
#include "qdebug.h"
#include "qdatetime.h"
#include "qpixmap.h"
#include "qscreen.h"
#include "QPaintEvent"
#include "qpainter.h"
#include "qcursor.h"
#include "QDesktopWidget"
#include "QSettings"
#include "QtNetwork/qtcpsocket.h"
#include "QHBoxLayout"
#include "qpushbutton.h"
#include "qlineedit.h"
#include "qcoreapplication.h"
#include "qmutex.h"
#include "qthread.h"
#include "qdatetime.h"
#include "QtEndian"

extern int resize_x;
extern int resize_y;
extern int xs;
extern int ys;
extern int xl;
extern int yl;
extern int pic_gap;
extern int bigger;

extern QMutex g_lock;
extern QString g_ip;
extern QImage g_img ;

extern int drawco;
class qthr:public QThread
{
    Q_OBJECT
public:
    int co=0;
QImage lastDrawIm ;
    qthr():QThread(){}
    void run()
    {
        while (1) {
            auto t1 = QDateTime::currentDateTime().toMSecsSinceEpoch();
            w();
            auto l=QDateTime::currentDateTime().toMSecsSinceEpoch()-t1;
            if(l<20)
                msleep(20-l);
        }
    }

    void set_bigger_pix(int i,int j,bool isBlack)
    {
        for(int ii=i*bigger;ii<(i+1)*bigger;ii++)
        {
            for(int jj=j*bigger;jj<(j+1)*bigger;jj++)
            {
                if(isBlack)
                {
                    lastDrawIm.setPixelColor(ii,jj,Qt::black);
                }
                else
                {
                    lastDrawIm.setPixelColor(ii,jj,Qt::white);
                }
            }
        }
    }
    void w()
    {
        co++;
        qDebug()<<"co"<<co;
        QTcpSocket* s = new QTcpSocket();
        s->connectToHost(g_ip,8999);
        auto xx =    s->waitForConnected(1000);
        if (!xx)
        {
            s->deleteLater();
            sleep(1);
            qDebug()<<"conn error";
            return;
        }

        QByteArray a;
        a.append((char*)&resize_x,4);
        a.append((char*)&resize_y,4);
        a.append((char*)&xs,4);
        a.append((char*)&ys,4);
        a.append((char*)&xl,4);
        a.append((char*)&yl,4);
        a.append((char*)&pic_gap,4);

        s->write(a.data(),a.size());
        s->waitForBytesWritten();
        a.clear();

        bool hasIni = false;
        int ty = 0;
        while (1) {
            s->waitForReadyRead();
            auto x = s->readAll();
            if(x.size()==0)break;

            if(!hasIni)
            {
                ty=x[0];
                x = x.right(x.size()-1);
                hasIni = true;
            }
            a.append(x);
        }

        s->deleteLater();

        QByteArray qq;
        quint32 ii = xl*yl;
        char cc[4];
        qToBigEndian(ii,cc);
        qq.append(cc,4);
        qq.append(a.data()+4,a.size()-4);
        if(ty==1)
        {
            a = qUncompress(qq);
        }

        if(a.size()!=xl*yl)
        {
            qDebug()<<"not equ"<<a.size();
            return;
        }

        lastDrawIm = QPixmap(QSize(xl*bigger,yl*bigger)).toImage();
        for (int i=0;i<xl;i++)
        {
            for(int j=0;j<yl;j++)
            {
                if (a[j*xl+i]==char(0))
                {
                         set_bigger_pix(i,j,true);
                }
                else {
                         set_bigger_pix(i,j,false);
                }
            }
        }
        g_lock.lock();
        drawco++;
        g_img = lastDrawIm;
        g_lock.unlock();

    }
};


#endif // UTIL_H
